package com.example.mannortycoon.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mannortycoon.dao.SeedUnlockConditionsDao;
import com.example.mannortycoon.entity.CropEntity;
import com.example.mannortycoon.entity.SeedEntity;
import com.example.mannortycoon.entity.SeedUnlockConditionsEntity;
import com.example.mannortycoon.entity.UserCropEntity;
import com.example.mannortycoon.service.CropService;
import com.example.mannortycoon.service.SeedService;
import com.example.mannortycoon.service.SeedUnlockConditionsService;
import com.example.mannortycoon.service.UserCropService;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.Query;
import com.example.mannortycoon.vo.UnlockConditionInfo;
import com.example.mannortycoon.vo.UnlockConditionInfo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("seedUnlockConditionsService")
public class SeedUnlockConditionsServiceImpl extends ServiceImpl<SeedUnlockConditionsDao, SeedUnlockConditionsEntity> implements SeedUnlockConditionsService {

    private final CropService cropService;

    private final UserCropService userCropService;

    @Autowired
    public SeedUnlockConditionsServiceImpl( CropService cropService,UserCropService userCropService){
        this.cropService = cropService;
        this.userCropService = userCropService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeedUnlockConditionsEntity> page = this.page(
                new Query<SeedUnlockConditionsEntity>().getPage(params),
                new QueryWrapper<SeedUnlockConditionsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<UnlockConditionInfo> getUnlockConditionsBySeedId(Long seedId) {
        QueryWrapper<SeedUnlockConditionsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("seed_id",seedId);
        List<SeedUnlockConditionsEntity> list = this.list(wrapper);
        return list.stream().map(item -> {
            UnlockConditionInfo unlockConditionInfo = new UnlockConditionInfo();
            CropEntity cropEntity = cropService.getById(item.getCropId());
            if (cropEntity != null) {
                unlockConditionInfo.setCropId(cropEntity.getCropId());
                unlockConditionInfo.setCropName(cropEntity.getCropName());
                unlockConditionInfo.setNumber(item.getNumber());
            }
            return unlockConditionInfo;
        }).collect(Collectors.toList());
    }

    @Override
    public void save(List<SeedUnlockConditionsEntity> unLockSeeds) {
        if(unLockSeeds != null){
            this.saveBatch(unLockSeeds);
        }
    }

    @Override
    @Transactional
    public void update(List<SeedUnlockConditionsEntity> unLockSeeds,Long seedId) {
        UpdateWrapper<SeedUnlockConditionsEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("seed_id",seedId);
        this.remove(wrapper);
        if(unLockSeeds != null){
            this.saveBatch(unLockSeeds);
        }
    }

    @Override
    public List<SeedUnlockConditionsEntity> listBySeedId(Long seedId) {
        QueryWrapper<SeedUnlockConditionsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("seed_id",seedId);
        return this.list(wrapper);
    }

    @Override
    public List<UnlockConditionInfo2> getUnlockSeedsWithUser(Long seedId, Long userId) {
        QueryWrapper<SeedUnlockConditionsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("seed_id",seedId);
        List<SeedUnlockConditionsEntity> list = this.list(wrapper);
        return list.stream().map(item->{
            UnlockConditionInfo2 unlockConditionInfo2 = new UnlockConditionInfo2();
            //获取作物信息
            CropEntity cropEntity = cropService.getById(item.getCropId());
            if(cropEntity == null){
                return null;
            }
            unlockConditionInfo2.setNumber(item.getNumber());
            unlockConditionInfo2.setCropImgUrl(cropEntity.getCropImgUrl());
            unlockConditionInfo2.setCropName(cropEntity.getCropName());
            //获取用户作物数量
            QueryWrapper<UserCropEntity> userCropEntityQueryWrapper = new QueryWrapper<>();
            userCropEntityQueryWrapper.eq("user_id",userId).eq("crop_id",cropEntity.getCropId());
            UserCropEntity userCropEntity = userCropService.getOne(userCropEntityQueryWrapper);
            if(userCropEntity != null){
                unlockConditionInfo2.setUserNumber(userCropEntity.getNumber());
            }else{
                unlockConditionInfo2.setUserNumber(0);
            }
            return unlockConditionInfo2;
        }).collect(Collectors.toList());
    }

}